import React, { Component } from 'react';

import './index.less';

import {
    base,
    toast,
    promptBox
} from 'nc-lightapp-front';

import checkSwitchToDoc from '../../request-functions/checkSwitchToDoc';

import language from '../../language';

const {
    NCModal,
    NCButton,
    NCCheckbox,
    NCPopconfirm
} = base;

const {
    Header: ModalHeader,
    Body: ModalBody,
    Footer: ModalFooter
} = NCModal;


class SwitchToDocModal extends Component {
    constructor(props) {
        super(props);

        this.state = {
            checkStep: 1,
            userTips2: '',
            userTips3: '',
            syncResume: false,
            checkboxReturn: ''
        };

        this.closeModal = this.closeModal.bind(this);
        this.handleSwitch = this.handleSwitch.bind(this);
        this.changeCheckboxHandle = this.changeCheckboxHandle.bind(this);
        this.checkStep2 = this.checkStep2.bind(this);
        this.checkStep3 = this.checkStep3.bind(this);
    }

    changeCheckboxHandle(value) {
        this.setState({
            syncResume: value
        });
    }

    async checkStep3() {
        let postData = {
            is_sync_work: this.state.syncResume,
            param_str: this.state.checkboxReturn,
            pk_org: this.props.humanResourceOrganizationValue.refpk
        };

        try {
            let res = await checkSwitchToDoc(postData, 3);

            if(res.success) {
                this.closeModal();
                toast({
                    color: 'success',
                    content: language['hi6007-000050']/* 国际化处理： 转入成功*/
                });
                this.props.getMainTableData();
            }
        }
        catch(e) {

        }
    }

    async checkStep2() {
        let postData = {
            param_str: this.state.checkboxReturn,
            pk_org: this.props.humanResourceOrganizationValue.refpk
        };

        try {
            let res = await checkSwitchToDoc(postData, 2);

            if(res.data.checkMsg) {
                promptBox({
                    color: 'warning',
                    title: language['hi6007-000008'], // 提示
                    content: res.data.checkMsg,
                    beSureBtnClick: () => {
                        this.setState({
                            checkboxReturn: res.data.passJobPK
                        }, () => {
                            this.checkStep3();
                        });
                    }
                });
            }
            else {
                this.setState({
                    checkboxReturn: res.data.passJobPK
                }, () => {
                    this.checkStep3();
                });
            }
        }
        catch (e) {
        }
    }

    async handleSwitch() {
        let data = this.props.editTable.getCheckedRows('ncc60071701list');
        let postData = {
            pk_org: this.props.humanResourceOrganizationValue.refpk,
            param_str: []
        };

        data.map((item) => {
            postData['param_str'].push(item.data.values.pk_psnjob.value);
        });

        try {
            let res = await checkSwitchToDoc(postData, 1);

            if (res.data.checkMsg) {
                promptBox({
                    color: 'warning',
                    title: language['hi6007-000008'], // 提示
                    content: res.data.checkMsg,
                    beSureBtnClick: () => {
                        this.setState({
                            checkboxReturn: res.data.passJobPK
                        }, () => {
                            this.checkStep2();
                        });
                    }
                });
            }
            else {
                this.setState({
                    checkboxReturn: res.data.passJobPK
                }, () => {
                    this.checkStep2();
                });
            }
        }
        catch (e) {
        }

    }

    closeModal() {
        this.props.updateState({
            switchToDocModalVisible: false
        });
        this.setState({
            checkStep: 1,
            userTips2: '',
            userTips3: '',
            syncResume: false,
            checkboxReturn: ''
        });
    }

    render() {
        const {
            switchToDocModalVisible
        } = this.props;

        const {
            userTips2,
            userTips3,
            syncResume,
            checkStep
        } = this.state;

        return (
            <NCModal
                show={switchToDocModalVisible}
                onHide={this.closeModal}
                size="sm"
                className="switch-to-doc-modal"
                backdrop="static"
            >
                <ModalHeader
                    closeButton={true}
                >
                    <span>{language['hi6007-000051']}</span>{/* 国际化处理： 确认转入*/}
                </ModalHeader>
                <ModalBody>
                    <div className="switch-to-doc-tips">{language['hi6007-000052']}?</div>{/* 国际化处理： 是否把所选择的人员转入人员档案*/}
                    <div className="switch-to-doc-checkbox-wraper">
                        <NCCheckbox
                            onChange={this.changeCheckboxHandle}
                            value={syncResume}
                        >
                            {language['hi6007-000053']}{/* 国际化处理： 同步履历*/}
                        </NCCheckbox>
                    </div>
                </ModalBody>
                <ModalFooter>
                    <NCButton
                        colors="primary"
                        onClick={this.handleSwitch}
                    >
                        {language['hi6007-000175']}{/* 国际化处理： 是*/}
                    </NCButton>
                    <NCButton
                        onClick={this.closeModal}
                    >
                        {language['hi6007-000176']}{/* 国际化处理： 否*/}
                    </NCButton>
                </ModalFooter>
            </NCModal>
        );
    }
}

export default SwitchToDocModal;
